let timeUtil = {
    //时间转换器
    chageTimeType(info) {
        let date = new Date(info);
        // 获取具体的年、月、日、时、分、秒
        let year = date.getFullYear();
        let month = date.getMonth() + 1; // 注意：月份从 0 开始，所以要加 1
        let day = date.getDate();
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let seconds = date.getSeconds();
        // 构造日期时间字符串
        // const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        return {
            year,
            month,
            day,
            hours,
            minutes,
            seconds
        }
    },

    //日期字符串补全
    formatTime(mn) {
        if (mn === '') return '未选择';
        // 分割输入字符串为小时和分钟
        let parts = mn.split(':');
        let M = parseInt(parts[0], 10); // 解析小时部分
        let n = parseInt(parts[1], 10); // 解析分钟部分
        // 确保小时和分钟都是两位数，不足补零
        let MM = M < 10 ? '0' + M : M.toString();
        let NN = n < 10 ? '0' + n : n.toString();
        // 返回格式化后的时间字符串
        return MM + ':' + NN;
    },

    //判断两个时间戳的时长 @return {time}
    timeDifference(start, end) {
        let diff = end - start;
        let days = Math.floor(diff / (24 * 3600 * 1000));
        diff = diff % (24 * 3600 * 1000);
        let hours = Math.floor(diff / (3600 * 1000));
        diff = diff % (3600 * 1000);
        let minutes = Math.floor(diff / (60 * 1000));
        diff = diff % (60 * 1000);
        let seconds = Math.floor(diff / 1000);
        return {
            days,
            hours,
            minutes,
            seconds
        }
    },
    getFormattedDate() {
        let today = new Date();
        let year = today.getFullYear();
        let month = today.getMonth() + 1; // getMonth() 返回的是从0开始的月份，所以要加1
        let day = today.getDate();

        // 将月份和日期转换为两位数
        if (month < 10) {
            month = '0' + month;
        }
        if (day < 10) {
            day = '0' + day;
        }

        return `${year}-${month}-${day}`;
    },
    formatDateTimeMM(timestamp) {
        if (typeof timestamp === 'string') {
            timestamp = Number(timestamp);
        }
        let date = new Date(timestamp * 1000); // 将秒数转换为毫秒数
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '; // 添加前导零
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y + M + D + h + m + s;
    },
    // 判断是否冲突会议时间
    isConflictWithAny(timestamp1, timestamp2, meetings) {
        // 遍历数组中的每个会议时间段
        for (let i = 0; i < meetings.length; i++) {
            const meeting = meetings[i];
            const start = meeting.MeetingBeginTime;
            const end = meeting.MeetingEndTime;

            // 检查给定的两个时间戳与当前会议时间段是否冲突
            if (timestamp1 < end && timestamp2 > start) {
                return true; // 时间冲突
            }
        }
        return false; // 没有冲突
    },
    isArryConflictWithAny(timestamps, meetings) {
    // 确保 timestamps 数组包含且仅包含两个时间戳
    if (timestamps.length !== 2) {
        throw new Error('Timestamps array must contain exactly two timestamps.');
    }
    const timestamp1 = timestamps[0];
    const timestamp2 = timestamps[1];

    // 遍历数组中的每个会议时间段
    for (let i = 0; i < meetings.length; i++) {
        const meeting = meetings[i];
        const start = meeting.MeetingBeginTime;
        const end = meeting.MeetingEndTime;

        // 检查给定的两个时间戳与当前会议时间段是否冲突
        if (timestamp1 < end && timestamp2 > start) {
            return false; // 时间冲突
        }
    }
    return true; // 没有冲突
    },
    //进度条时间判断
    //形参 "time": [{"MeetingBeginTime": 1721686326, "MeetingEndTime": 1721686326 }] 
    // @return: [{占用状态0/1,占用的时间点数"number-number"}] 
    getTimeOccupancyArray(timeArray) {
        // 初始化长度为 24 的数组，全部填充为对象，初始状态为未被占用 (0) 和对应的时间段（小时数）
        let resultArray = new Array(24).fill().map((_, index) => ({
            occupied: 0,
            timePoint: `${index}`  // 对应的时间段，例如 0-1, 1-2, ..., 23-24
        }));
    
        // 遍历传入的时间戳数组
        timeArray.forEach(meeting => {
            // 将会议开始时间和结束时间转换为小时数
            let startHour = new Date(meeting.MeetingBeginTime * 1000).getHours();
            let endHour = new Date(meeting.MeetingEndTime * 1000).getHours();
    
            // 标记占用的时间段为 1
            for (let i = startHour; i <= endHour && i < resultArray.length; i++) {
                resultArray[i].occupied = 1;
            }
        });
    
        return resultArray;
    }
    
    
}

export default timeUtil;